﻿Imports Microsoft.VisualBasic
Imports System.Collections.Generic
Imports System.Linq
Namespace OneCall.User
    Public Class AuthUser
        Dim Req As System.Web.HttpRequest
        Dim Res As System.Web.HttpResponse
        Public Sub New(ByVal Request As HttpRequest)
            Req = Request
        End Sub
        Public Sub New(ByVal Response As HttpResponse)
            Res = Response
        End Sub
        Public Sub New(ByVal Request As HttpRequest, ByVal Response As HttpResponse)
            Res = Response
            Req = Request
        End Sub
        Public Sub RegisterUser(ByVal UserID As Integer)
            Dim sessonCookie As New HttpCookie("1CSESSON")
            Dim key As String = Guid.NewGuid().ToString()
            sessonCookie.Values("k") = key
            sessonCookie.Values("_uuid_") = OneCall.Crypt.Encrypt(UserID, key)
            sessonCookie.Values("_ssn_") = OneCall.Crypt.Encrypt("Active", key)
            sessonCookie.Expires = Now().AddHours(1)
            Res.Cookies.Add(sessonCookie)
            Res.Redirect("custHome.aspx")
        End Sub
        Public Function isActiveSession() As Boolean 'Request
            If Not Req.Cookies("1CSESSON") Is Nothing Then
                Dim key As String = Req.Cookies("1CSESSON")("k")
                If Req.Cookies("1CSESSON")("_ssn_") = OneCall.Crypt.Encrypt("Active", key) Then
                    Return True
                Else
                    Return False
                End If
            Else
                Return False
            End If
        End Function
        Public Function GetUserID() As Integer
            Dim ret As Integer = 0
            If isActiveSession() Then
                Dim key As String = Req.Cookies("1CSESSON")("k")
                ret = OneCall.Crypt.Decrypt(Req.Cookies("1CSESSON")("_uuid_"), key)
            End If
            Return ret
        End Function
        Public Function GetEmailID() As String
            Dim ret As String = ""
            If isActiveSession() Then
                Dim SQLDB As New OneCall.DataModel.OneCDataEntities
                Dim uid As Integer = GetUserID()
                Dim Customers As List(Of OneCall.DataModel.Customer) = (From p In SQLDB.Customers Where p.Customer_ID = uid).ToList()
                ret = Customers(0).Customer_EmailID
            End If
            Return ret
        End Function
        Public Function getMobileNumber() As Integer
            Dim ret As Integer = 0
            If isActiveSession() Then
                Dim SQLDB As New OneCall.DataModel.OneCDataEntities
                Dim uid As Integer = GetUserID()
                Dim Customers As List(Of OneCall.DataModel.Customer) = (From p In SQLDB.Customers Where p.Customer_ID = uid).ToList()
                ret = Customers(0).Customer_Mobile
            End If
            Return ret
        End Function
        Public Sub SignOut() 'Response
            Dim key As String = Req.Cookies("1CSESSON")("k")
            Res.Cookies("1CSESSON")("_ssn_") = ""
            Res.Cookies("1CSESSON").Values.Clear()
            Res.Cookies("1CSESSON").Expires = Now().AddDays(-1)

        End Sub
        Public Function GetUserFullName() As String
            Try
                Dim key As String = Req.Cookies("1CSESSON")("k")
                Dim user_id As Integer = FormatNumber(OneCall.Crypt.Decrypt(Req.Cookies("1CSESSON")("_uuid_"), key))
                Dim SQLDB As New OneCall.DataModel.OneCDataEntities
                Dim Customers As List(Of OneCall.DataModel.Customer) = (From p In SQLDB.Customers Where p.Customer_ID = user_id).ToList()
                Return Customers(0).Customer_FirstName & " " & Customers(0).Customer_LastName
            Catch ex As Exception
                Return ""
            End Try
        End Function
    End Class
End Namespace
